/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mobiubiq.directory.client.frame;

import javax.microedition.contactless.ContactlessException;
import javax.microedition.contactless.TargetListener;
import javax.microedition.contactless.TargetProperties;
import javax.microedition.contactless.ndef.NDEFMessage;
import javax.microedition.contactless.ndef.NDEFRecordListener;
import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.util.frame.FrameHandler;
import org.mobiubiq.directory.client.ContactlessUtils;
import org.mobiubiq.directory.client.object.AbstractObject;
import org.mobiubiq.directory.client.object.TagObject;

/**
 *
 * @author Marnix Renne
 */
public class NfcReader extends ScreenDefinitionBaseFrame implements TargetListener {

    public NfcReader() {
        super("read-nfc.xml");
        try {
            ContactlessUtils.registerTargetListeners(this);
        } catch (ContactlessException ex) {
            Kuix.alert(ex);
        }
    }

    public boolean onMessage(Object identifier, Object[] arguments) {
        boolean result = super.onMessage(identifier, arguments);
        if (result) {
            if ("targetDetected".equals(identifier)) {
                ContactlessUtils.unregisterTargetListeners(this);
                this.remove();
                AbstractObject object =  (AbstractObject) arguments[0];
                FrameHandler handler = Kuix.getFrameHandler();
                handler.pushFrame(new Services(object));
                result = false;
            }
        } else {
            ContactlessUtils.unregisterTargetListeners(this);
        }
        return result;
    }

    public void targetDetected(TargetProperties[] targetProperties) {
        this.onMessage("targetDetected", new Object[]{new TagObject(targetProperties)});
    }
}
